<!--维修车辆管理页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆维修</span>
    <el-button @click="beforeAddRepairvehicle" type="primary" style="float:right;margin-top:13px;" >录入维修车辆</el-button>
  </div>
  <!-- 新增维修车辆弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible"
             style="width:1000px;padding:40px;" :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="送修人">
            <el-input disabled :value="user.username"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修车辆">
            <el-select placeholder="请选择车牌号" v-model="saveRepairvehicleForm.vehicleId">
              <el-option v-for="item in vehicleList" :label="item.license" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修项目">
            <el-select placeholder="请选择" v-model="saveRepairvehicleForm.item">
              <el-option v-for="item in itemOptions" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修地点">
            <el-select placeholder="请选择" v-model="saveRepairvehicleForm.addr">
              <el-option v-for="item in addrOptions" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修材料">
            <el-select placeholder="请选择" v-model="saveRepairvehicleForm.material">
              <el-option v-for="item in materialOptions" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修费用">
            <el-input placeholder="请输入" v-model="saveRepairvehicleForm.fee">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
                            v-model="saveRepairvehicleForm.repairTime" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修状态">
            <el-radio-group v-model="saveRepairvehicleForm.status">
              <el-radio label="维修中" value="1" style="margin: 0;"></el-radio>
              <el-radio label="已完成" value="0"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveRepairvehicle">确定</el-button>
    </template>
  </el-dialog>
  <!-- 维修车辆搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="维修车辆">
        <el-input placeholder="请输入车牌号" style="width:220px;"
                  v-model="searchRepairvehicleForm.vehicleLicense" @keydown.enter="loadRepairvehicle"></el-input>
      </el-form-item>
      <el-form-item label="送修人员" style="width:290px;">
        <el-input placeholder="请输入送修人姓名" style="width:220px;"
                  v-model="searchRepairvehicleForm.username" @keydown.enter="loadRepairvehicle"></el-input>
      </el-form-item>
      <el-form-item label="维修状态" style="width:290px;">
        <el-select placeholder="请选择维修状态" style="width:220px;"
                  v-model="searchRepairvehicleForm.status" >
          <el-option label="维修中" value="1"></el-option>
          <el-option label="已完成" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadRepairvehicle">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 维修车辆列表 -->
  <el-card style="margin:20px;">
    <el-table :data="repairVehicleArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="vehicleLicense" label="维修车辆" align="center"/>
      <el-table-column prop="item" label="维修项目" align="center" :formatter="itemFormatter"/>
      <el-table-column prop="username" label="送修人员" align="center"/>
      <el-table-column prop="addr" label="维修地点" align="center"
                       :formatter="addrFormatter"/>
      <el-table-column prop="repairTime" label="维修时间" align="center"/>
      <el-table-column prop="fee" label="维修费用" align="center"/>
      <el-table-column prop="material" label="维修材料" align="center" :formatter="materialFormatter"/>
      <el-table-column prop="status" label="维修状态" align="center" :formatter="statusFormatter"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editRepairvehicle(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small"
           @click="deleteRepairvehicle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";

const user=ref(getUser());
//控制新增维修车辆弹窗是否显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('录入维修车辆');


//定义对象用来保存维修车辆的搜索条件
const searchRepairvehicleForm = ref({vehicleLicense:'',username:'',status:''});
//定义数组对象用来保存维修车辆列表的数据
const repairVehicleArr = ref([
  {
    license: '京A22323',
    item: '汽车保养',
    username: 'mike',
    addr: '沃尔沃专修店',
    repairTime: '2021-01-01',
    fee: '10034.45',
    material: '汽缸垫进排气歧管垫',
    status: '维修中'
  },
  {
    license: '京B22323',
    item: '汽车美容',
    username: 'lucy',
    addr: '美奥行汽修',
    repairTime: '2024-11-23',
    fee: '613.27',
    material: '气门弹簧',
    status: '维修中'
  },
  {
    license: '京C22323',
    item: '钣金喷漆',
    username: 'tom',
    addr: '悦心金峰盛车检汽修',
    repairTime: '2022-12-21',
    fee: '2653.5',
    material: '板簧胶套',
    status: '已完成'
  }
]);
//定义加载维修车辆的方法
const loadRepairvehicle=()=>{
  let data = qs.stringify(searchRepairvehicleForm.value);
  axios.get(BASE_URL+'/v1/repair/select?'+data).then((response)=>{
    if(response.data.code==2000){
      repairVehicleArr.value=response.data.data;
    }
  })
  loadVehicle();
}
//定义重置搜索的方法
const resetSearch=()=>{
  searchRepairvehicleForm.value={};
  loadRepairvehicle();
}
//一进到页面立即执行调用加载维修车辆的方法
onMounted(()=>{
  loadRepairvehicle();
  loadDictOptions(materialOptions,'repair_material');
  loadDictOptions(itemOptions,'repair_item');
  loadDictOptions(addrOptions,'repair_addr');
})
const materialOptions=ref([]);
//维修材料显示转换
const materialFormatter =(row,column,cellValue,index)=>{
  if(!materialOptions.value){
    return cellValue;
  }
  for(let item of materialOptions.value){
    if(item.value==cellValue){
      cellValue=item.label;
    }
  }
  return cellValue;
}
const itemOptions=ref([]);
//维修项目显示转换
const itemFormatter = (row,column,cellValue,index)=>{
  //如果字典项数组没有值，直接返回当前单元格的值，比如10
  if(!itemOptions.value){
    return cellValue;
  }
  //依次遍历出字典项数组中的每个字典项
  for(let item of itemOptions.value){
    //如果字典项的value值与当前单元格的值一致
    if(item.value==cellValue){
      //就把当前单元格的值改为此字典项的label值，比如 轿车
      cellValue = item.label;
    }
  }
  return cellValue;
}
const addrOptions=ref([]);
//维修地址显示转换
const addrFormatter = (row,column,cellValue,index)=> {
  if(!addrOptions.value){
    return cellValue;
  }
  for(let item of addrOptions.value){
    if(item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}

//定义对象用来保存新增维修车辆弹窗表单的所有数据
const saveRepairvehicleForm = ref({
  vehicleId:'',
  item:'',
  addr:'',
  material:'',
  fee:'',
  repairTime:''
});

const vehicleList=ref([]);
const loadVehicle = ()=>{
  axios.get(BASE_URL+'/v1/vehicle/select?status=1').then((response)=>{
    if (response.data.code==2000){
      vehicleList.value = response.data.data;
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}

//定义保存维修车辆的方法
const saveRepairvehicle = ()=>{
  saveRepairvehicleForm.value.userId=user.value.id;
  saveRepairvehicleForm.value.username=user.value.username;
  let data = qs.stringify(saveRepairvehicleForm.value);
  axios.post(BASE_URL+'/v1/repair/save',data).then((response)=>{
    if(response.data.code==2000){
      ElMessage.success('录入成功！');
      //保存成功后：关弹窗+清除表单数据+重新加载所有车辆数据（含刚刚新增的那条车辆数据）
      dialogVisible.value = false;
      saveRepairvehicleForm.value = {};
      loadRepairvehicle();
    }
  })
}
//定义关闭保存维修车辆弹窗的方法
const handleClose = ()=>{
  if(confirm('您确认要关闭本弹窗吗？')){
    dialogVisible.value = false;//关闭弹窗
    saveRepairvehicleForm.value = {};//清空表单数据
  }
}
//定义点击“录入维修车辆”按钮时调用的方法 出弹窗+改标题+清空表单数据
const beforeAddRepairvehicle =()=>{
  dialogVisible.value = true;
  dialogTitle.value = '录入维修车辆';
  saveRepairvehicleForm.value = {};
}
//定义编辑维修车辆的方法 出弹窗+改标题+回显数据
const editRepairvehicle=(id)=>{
  dialogVisible.value = true;
  dialogTitle.value = '编辑维修车辆';
  //根据当前车辆id回显弹窗表单中此车辆的详细数据
  axios.get(BASE_URL+'/v1/repair/select?id='+id).then((response)=>{
    if(response.data.code==2000){
      saveRepairvehicleForm.value = response.data.data[0];
    }
  })
}
//定义删除维修车辆的方法
const deleteRepairvehicle =(id)=>{
  if(confirm('您确认删除该维修的车辆信息吗？')){
    axios.post(BASE_URL+'/v1/repair/delete/'+id).then((response)=>{
      if(response.data.code==2000){
        ElMessage.success('删除成功！')
        loadRepairvehicle();
      }else{
        ElMessage.error(response.data.msg);
      }
    })
  }
}
const statusFormatter=(row,column,cellValue,index)=>{
  if(cellValue==1){
    cellValue='维修中';
  }else{
    cellValue='已完成';
  }
  return cellValue;
}
const loadDictOptions = (object,dictCode)=>{
    axios.get(BASE_URL+'/v1/dictOption/select/'+dictCode).then((response)=>{
    if(response.data.code==2000){
      object.value = response.data.data;
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
</script>